package com.coolgor.coldot.service.wechat.gzh.impl;

import com.coolgor.coldot.common.wechat.HttpUtil;
import com.coolgor.coldot.dto.wechat.gzh.GZHAccessTokenExecution;
import com.coolgor.coldot.enums.wechat.gzh.GZHAccessTokenOperationStateEnum;
import com.coolgor.coldot.service.wechat.gzh.GZHAccessTokenService;
import com.coolgor.coldot.service.wechat.gzh.GZHMessageService;
import com.coolgor.coldot.service.wechat.gzh.GZHMessageTemplateService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class GZHMessageServiceImpl implements GZHMessageService {

    @Autowired
    private GZHAccessTokenService gzhAccessTokenService;

    @Autowired
    private GZHMessageTemplateService gzhMessageTemplateService;

    @Override
    public String sendTemplateMessage(Long recordID,
                                      String templateName,
                                      String toUser,
                                      String msg) {
        // 根据recordID从数据库中获取记录
        GZHAccessTokenExecution gzhAccessTokenExecution = gzhAccessTokenService.getGZHAccessToken(recordID);
        if (gzhAccessTokenExecution.getState() != GZHAccessTokenOperationStateEnum.SUCCESS.getState()) {
            return null;
        }
        return this.sendTemplateMessage(gzhAccessTokenExecution.getGZHAccessToken().getAppID(),
                templateName, toUser, null, msg, null, null);
    }

    @Override
    public String sendTemplateMessage(String appID,
                                      String templateName,
                                      String toUser,
                                      String title,
                                      String msg,
                                      String remarkMsg,
                                      String jumpto) {

        String accesstoken = gzhAccessTokenService.getGZHAccessToken(appID).getGZHAccessToken().getAccessToken();
        String templateID = gzhMessageTemplateService.getTemplateID(appID, templateName);

        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send" +
                "?access_token=" + accesstoken;

        ObjectMapper mapper = new ObjectMapper();//先创建objmapper的对象
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("touser", toUser);
        args.put("template_id", templateID);
//        args.put("topcolor", "#FF0000");
        if (null!=jumpto && !"".equals(jumpto)) {
            args.put("url", jumpto);
        }

        Map<String, Object> data = new HashMap<String, Object>();

        if (null!=title && !"".equals(title)) {
            Map<String, String> first = new HashMap<String, String>();
            first.put("value", title);
            first.put("color", "#173177");
            data.put("first", first);
        }

        Map<String, String> keyword1 = new HashMap<String, String>();
        keyword1.put("value", msg);
        keyword1.put("color", "#173177");
        data.put("keyword1", keyword1);

        Map<String, String> keyword2 = new HashMap<String, String>();
        keyword2.put("value", new Date().toString());
        keyword2.put("color", "#173177");
        data.put("keyword2", keyword2);

        if (null!=remarkMsg && !"".equals(remarkMsg)) {
            Map<String, String> remark = new HashMap<String, String>();
            remark.put("value", remarkMsg);
            remark.put("color", "#173177");
            data.put("remark", remark);
        }

        args.put("data", data);

        String outputData = null;
        try {
            outputData = mapper.writeValueAsString(args);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        String resStr = HttpUtil.httpsRequest(url, "POST", outputData);
        return resStr;
    }
}
